package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.jt1078.entity.JT_8103;
import com.uzhie.jt808.jt1078.entity.JT_8103_Catalog;
import com.uzhie.jt808.jt1078.entity.param8103.JT_8103_param_storge_setting;
import com.uzhie.jt808.jt1078.entity.param8103.JT_8103_param_terminal_pause;
import com.uzhie.jt808.jt1078.entity.param8103.JT_8103_param_video_setting;
import com.uzhie.jt808.jt1078.entity.param8103.JT_8103_param_warn;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.service.JT8103Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangdequan on 2018/8/13.
 */

@RestController
@RequestMapping(value = "jt8103")
public class JT8103Controller extends CommController {

    @Autowired
    private JT8103Service jt8103Service;

    @ResponseBody
    @RequestMapping(value = "order")
    public PayloadBean monitorOrder(@RequestBody(required = false) JSONObject body) {
        try {
            JT_8103_Catalog jt_8103_catalog = new JT_8103_Catalog();

            //音视频参数设置指令
            JT_8103_param_video_setting jt_8103_param_video_setting;
            //特殊报警录像参数设置
            JT_8103_param_warn jt_8103_param_warn;
            //终端休眠唤醒模式设置
            JT_8103_param_terminal_pause jt_8103_param_terminal_pause;
            //音视频存储流参数设置
            JT_8103_param_storge_setting jt_8103_param_storge_setting;
            //指令id
            int id = body.getIntValue("id");
            jt_8103_catalog.setID(BitConverter.GetBytes(id));
            //获取下发终端的sim卡号
            JSONArray simNumber = body.getJSONArray("simNumber");

            for (int h = 0; h < simNumber.size(); h++) {
                Object obj = simNumber.get(h);
                //根据id,封装不同的消息体
                switch (id) {
                    case 0x0075:
                        jt_8103_param_video_setting = JSON.toJavaObject(body.getJSONObject("param"), JT_8103_param_video_setting.class);
                        jt_8103_catalog.setID(BitConverter.GetBytes(0x0075));
                        jt_8103_catalog.setLength(jt_8103_param_video_setting.WriteToBytes().length);
                        jt_8103_catalog.setParam(jt_8103_param_video_setting.WriteToBytes());
                        break;
                    case 0x0076:
                        JSONArray jsonArray = body.getJSONObject("param").getJSONArray("subtitle_setting");
                        short subtitle_setting = BitConverter.ToUShortList(jsonArray);
                        body.getJSONObject("param").remove("subtitle_setting");
                        jt_8103_param_storge_setting = JSON.toJavaObject(body.getJSONObject("param"), JT_8103_param_storge_setting.class);
                        jt_8103_param_storge_setting.setSubtitle_setting(subtitle_setting);

                        jt_8103_catalog.setID(BitConverter.GetBytes(0x0075));
                        jt_8103_catalog.setLength(jt_8103_param_storge_setting.WriteToBytes().length);
                        jt_8103_catalog.setParam(jt_8103_param_storge_setting.WriteToBytes());
                        break;
                    case 0x0079:
                        jt_8103_param_warn = JSON.toJavaObject(body.getJSONObject("param"), JT_8103_param_warn.class);
                        jt_8103_catalog.setID(BitConverter.GetBytes(0x0079));
                        jt_8103_catalog.setLength(jt_8103_param_warn.WriteToBytes().length);
                        jt_8103_catalog.setParam(jt_8103_param_warn.WriteToBytes());
                        break;
                    case 0x007c:
                        jt_8103_param_terminal_pause = JSON.toJavaObject(body.getJSONObject("param"), JT_8103_param_terminal_pause.class);
                        jt_8103_catalog.setID(BitConverter.GetBytes(0x007c));
                        jt_8103_catalog.setLength(jt_8103_param_terminal_pause.WriteToBytes().length);
                        jt_8103_catalog.setParam(jt_8103_param_terminal_pause.WriteToBytes());
                        break;
                    default:
                        throw new Exception("指令id发送有误!!");
                }

                //封装参数列表
                List<JT_8103_Catalog> list = new ArrayList<>();
                list.add(jt_8103_catalog);
                JT_8103 jt8103 = new JT_8103(list);
                //调用下发命令
                String result = jt8103Service.terminalPause(JT808Constant.MSG_ID.PLA_REQ_SET_PARAM, obj.toString(), jt8103);
                System.out.println(result);
            }
            return buildResponse(null);
        } catch (Exception e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }
}
